package com.czkevin.guicontrolsapi.controls;

import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;

public class BlinkingLightControl extends GUIControl {

    private final ItemStack statusOffItem;
    private final ItemStack statusOnItem;
    private final long minimal_interval;

    private boolean status = false;
    private long last_update = 0;

    public BlinkingLightControl(ItemStack statusOffItem, ItemStack statusOnItem, long minimal_interval) {
        this.statusOffItem = statusOffItem;
        this.statusOnItem = statusOnItem;
        this.minimal_interval = minimal_interval;
    }

    @Override
    public void onClick(InventoryClickEvent event, int localX, int localY) { }

    @Override
    public int getWidth() {
        return 1;
    }

    @Override
    public int getHeight() {
        return 1;
    }

    @Override
    public void onRender(boolean force) {
        if(!force && System.currentTimeMillis() - last_update < minimal_interval) return;
        if(System.currentTimeMillis() - last_update >= minimal_interval) {
            if (status) {
                getWindow().setDisplayItemAt(getX(), getY(), statusOnItem);
            } else {
                getWindow().setDisplayItemAt(getX(), getY(), statusOffItem);
            }
            status = !status;
            last_update = System.currentTimeMillis();
        }
    }
}
